package algocasts;

import java.util.Arrays;
import java.util.Random;

public class P58_Shuffle {
    private Random random = new Random();

    void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;


    }

    // Time: O(n), Space:O(1)
    public int[] shuffle(int[] nums) {
        for (int i = nums.length - 1; i > 0; i--) {
            int j = random.nextInt(i + 1);
            swap(nums, i, j);
        }
        return nums;
    }
}
